package com.logic.modular.gameCore.match.core.service.impl;

import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logic.modular.gameCore.match.core.mapper.CoreMatchEndExpMapper;
import com.logic.modular.gameCore.match.core.domain.CoreMatchEndExp;
import com.logic.modular.gameCore.match.core.service.CoreMatchEndExpService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * (CoreMatchEndExp)表服务实现类
 *
 * @author cxy
 * @since 2024-09-10 11:26:03
 */
@Slf4j
@Service
public class CoreMatchEndExpServiceImpl extends ServiceImpl<CoreMatchEndExpMapper, CoreMatchEndExp> implements CoreMatchEndExpService {
    Cache<String, Map<Long,CoreMatchEndExp>> cache = Caffeine.newBuilder().build();
    
    @PostConstruct
    private void init() {
        CoreMatchEndExp initSelect = CoreMatchEndExp.builder().build();
        List<CoreMatchEndExp> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存数据,数据量为:{}",list.size());
        cache.put(
                "coreMatchEndExp", 
                list.stream().collect(Collectors.toMap(CoreMatchEndExp::getId, CoreMatchEndExp::init))
        );
        log.info("数据缓存完成");
    }

    public List<CoreMatchEndExp> listCache() {
        Map<Long,CoreMatchEndExp> values = cache.getIfPresent("coreMatchEndExp");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return new ArrayList<>(values.values());
    }     
    
    public Map<Long,CoreMatchEndExp> mapCache() {
        Map<Long,CoreMatchEndExp> values = cache.getIfPresent("coreMatchEndExp");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }   
    
    public CoreMatchEndExp getCache(Long id) {
        Map<Long,CoreMatchEndExp> values = cache.getIfPresent("coreMatchEndExp");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values.get(id);
    }

    public List<CoreMatchEndExp> listCache(int type) {
        return listCache().stream().filter(item -> item.getType() == type).collect(Collectors.toList());
    }
}
